package inutan.game.event;

import inutan.game.business.ActorManager;
import inutan.game.service.TaskService;
import inutan.game.event.model.*;
import org.ricks.common.actor.Actor;
import org.ricks.common.actor.Actors;
import org.ricks.common.event.Event;
import org.ricks.common.event.EventHandler;
import org.ricks.common.lang.Logger;
import org.ricks.ioc.RicksContext;
import org.ricks.ioc.anno.Autowired;
import org.ricks.ioc.anno.PreDestroy;

@Event
public class EventModel {

    /**
     * 事件机制独立化
     * 和IOC模块分离。所以不能使用依赖注入方式 注入Bean
     */
    @Autowired
    private TaskService iocTaskService;

    @EventHandler(async = false)
    public void loginTask(LoginEvent loginEvent) {
        TaskService taskService = RicksContext.me().getInstance(TaskService.class);
        Actor actor = Actors.me().get(loginEvent.getUserId());
        Logger.info(" curr thread name :"+Thread.currentThread().getName()+" & 开始投递给业务线程");
        actor.execute("login-task",() -> doLoginTask("loginTask"));
    }


    @EventHandler
    public void loginDelayTask(LoginDelayEvent loginEvent) {
       Logger.info(Thread.currentThread().getName() + "进入延迟任务 & 可以处理种菜成熟通知客户端");
    }

    private void doLoginTask(String name) {
        Logger.info(Thread.currentThread().getName() + "开始处理登录事件。。。" + name);
    }

    @EventHandler
    public void completeTask(CompleteTaskEvent completeTaskEvent){
        TaskService taskService = RicksContext.me().getInstance(TaskService.class);
        Actor actor = Actors.me().get(completeTaskEvent.getUserId());
        actor.execute("complete-task",() -> taskService.doNextTask(completeTaskEvent.getTaskId(),completeTaskEvent.getUserId()));
    }

    /**
    * @Explain --> 玩家升级事件
    * @Author yanwang
    * @Date 2023/4/10 13:41
    **/
    @EventHandler
    public void roleLevelUpTask(RoleLevelUpEvent roleLevelUpEvent){
//        ConditionManager.condition(roleLevelUpEvent); //发起条件机制
        TaskService taskService = RicksContext.me().getInstance(TaskService.class);
        taskService.monitorRoleLevelUp(roleLevelUpEvent.getContext(),roleLevelUpEvent.getLv(), roleLevelUpEvent.getOldLv());
    }
    /**
    * @Explain --> 获得道具事件
    * @Author yanwang
    * @Date 2023/4/10 13:42
    **/
    @EventHandler
    public void addItemTask(AddItemEvent addItemEvent){
        TaskService taskService = RicksContext.me().getInstance(TaskService.class);
        Actor actor = Actors.me().get(addItemEvent.getContext().getUserId());
        actor.execute("add-item", () -> taskService.monitorAddItem(addItemEvent.getContext(),addItemEvent.getList()));
    }

    /**
     * 进程stop销毁事件
     */
    @PreDestroy
    public void shutdown() {
        Logger.info("进程开始销毁。。。。。1");
        ActorManager.get().stop(); //销毁自定义actor
//        GatewayManager.me().stop();
//        RedisUtils.hdel(RedisKeys.businessServer,String.valueOf(ConfigConstant.ID)); //用于内网登录服 获取运行的业务节点 下拉框
        /**
         * 通知登录服解除绑定
         * 解除 user  <->  businessId 绑定关系
         */
//        changeBindBusiness(0,ConfigConstant.ID);
    }

    //    public static void changeBindBusiness(long userId,int businessId) {
//        String baseUrl = RicksConf.get("game.changeBindBusinessUrl");
//        ActionErrorEnum.systemOtherErrCode.assertTrue(!baseUrl.isBlank()," Game config error ,检查配置文件是否配置了game.changeBindBusinessUrl ");
//        /**
//         * 通知登录服解除绑定
//         * 解除 user  <->  businessId 绑定关系
//         */
//        Map<String,Object> map = new HashMap();
//        map.put("businessId", businessId);
//        map.put("userId", userId);
//        //游戏服 stop 异步通信登录服 账号解除游戏节点绑定
//        HttpUtils.doAsyncPost(baseUrl, new JSONObject(map).toString());
//    }
}
